package com.sharepics.ws.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;

import com.sharepics.bean.Album;
import com.sharepics.bean.Picture;
import com.sharepics.bean.XMLAlbum;
import com.sharepics.bean.XMLAlbums;
import com.sharepics.bean.XMLPicture;
import com.sharepics.utils.bean.BeanUtils;
import com.thoughtworks.xstream.XStream;

public class WebServiceClient {

	
	
	public static XMLAlbum createAlbum(Album album) throws IllegalStateException, IOException{
		
		XStream xstream = new XStream();
		xstream.alias("album", XMLAlbum.class);
		XMLAlbum xmlAlbum = BeanUtils.get(album);
		String xml = xstream.toXML(xmlAlbum);
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost postRequest = new HttpPost(
				"http://10.0.2.2:8080/ws/album.action");
    	postRequest.setHeader("Accept", "application/xml");
    	postRequest.setHeader("Content-type", "application/xml");
		try {
			postRequest.setEntity(new StringEntity(xml));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		HttpResponse response = null;
		try {
			response = httpClient.execute(postRequest);
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		

		
		 // Get hold of the response entity
		 HttpEntity entity = response.getEntity();

		 String xmlAlbumReturn = IOUtils.toString(entity.getContent(),"UTF-8");
		 xmlAlbum =  (XMLAlbum) xstream.fromXML(xmlAlbumReturn);
		 return xmlAlbum;
		
	}
	
	public static XMLAlbums getAlbums() throws IllegalStateException, IOException{
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpGet getRequest = new HttpGet(
				"http://10.0.2.2:8080/ws/albums.action");
		getRequest.setHeader("Accept", "application/xml");
		getRequest.setHeader("Content-type", "application/xml");
		HttpResponse response = null;
		try {
			response = httpClient.execute(getRequest);
		} catch (ClientProtocolException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		HttpEntity entity = response.getEntity();
		String xmlAlbumReturn = IOUtils.toString(entity.getContent(),"UTF-8");
		XStream xstream = new XStream();
		xstream.alias("albums", XMLAlbums.class);
		xstream.alias("album", XMLAlbum.class);
		xstream.addImplicitCollection(XMLAlbums.class, "albums");
		XMLAlbums xmlAlbums =  (XMLAlbums) xstream.fromXML(xmlAlbumReturn);
		return xmlAlbums;
		
	}
	
	
	
	public static XMLPicture createPicture(Picture picture,InputStream is) throws ClientProtocolException, IOException{
		
		
		
		XStream xstream = new XStream();
		xstream.alias("picture", XMLPicture.class);
		
		byte[] data = null;
		data = IOUtils.toByteArray(is);
		
		
		XMLPicture xmlPicture = BeanUtils.get(picture,data);
		String xml = xstream.toXML(xmlPicture);
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost postRequest = new HttpPost(
				"http://10.0.2.2:8080/ws/picture.action");
    	postRequest.setHeader("Accept", "application/xml");
    	postRequest.setHeader("Content-type", "application/xml");
		try {
			postRequest.setEntity(new StringEntity(xml));
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		HttpResponse response = null;
		response = httpClient.execute(postRequest);
		
		return xmlPicture;
		
	}
}
